import sys,os

try:
 	import pygtk
  	pygtk.require("2.0")
except:
  	pass
try:
	import gtk
  	import gtk.glade
except:
	sys.exit(1)

def getlibpath(file):
    for i in sys.path:
        #print i+file
        if os.path.exists(i+file):
            return i+file
	
class WHolder:
    def __init__(self,idx,sb_cb):
        self.idx = idx
        self.sb_cb = sb_cb
        self.scrolling = False
        self.inited = False
        self.wTree = gtk.glade.XML( getlibpath("/pavol/vbox.glade") )
        self.vbox = self.wTree.get_widget("vbox1")
        self.vbox.set_size_request(150,75)
        self.scrollbar = self.wTree.get_widget("vsbvolume")
        #self.scrollbar.set_increments(1,100)
        self.scrollbar.set_update_policy(gtk.UPDATE_CONTINUOUS)
        self.scrollbar.connect("value-changed",self.sb_valchanged);
        self.scrollbar.connect("move-slider",self.sb_moveslider);
        self.wTree.get_widget("cbmute").connect("clicked",self.mute_cb)
        #self.pamodule = None
        
    def get_widget(self):
        return self.vbox
        
    def set_label(self,label):
        self.wTree.get_widget("labelapp").set_label(label)
        
    def set_desc(self,label):
        self.wTree.get_widget("labeldesc").set_label(label)
    
    def sb_moveslider(self,sb,type):
        print sb.get_value() , type
        
    def sb_valchanged(self,sb): #Check that no recursive loop would slip in with set_sbvalue
        if self.inited:
            #print "Scroll:", sb.get_value()
            self.scrolling = True
            self.sb_cb(self.idx, (sb.get_value() / 100.0) * 65535 )
        else:
            self.inited = True #New sink is added and scrollbar value set, ignore it
    
    def set_sbvalue(self,val):
        if self.scrolling:
            print "Setting scroll"
            self.scrolling = False
            return
        #self.inited = False
        print "Scroll set val"
    	self.scrollbar.set_value( (val/65535.0) * 100.0)
    
    def mute_cb(self,mute):
        print mute.get_active()
        
